﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ComponentModel;
using System.Linq.Expressions;

using Zorbo.Interface;

namespace Zorbo.Shell
{
    /// <summary>
    /// Our test implementation of IServerConfig, IServerConfig also implements INotifyPropertyChanged for maximum 
    /// compatibility between multiple development architectures (WinForms, WPF, MONO, etc), INotifyPropertyChanged can also 
    /// be subscribed to by plugins to listen for property changes
    /// </summary>
    public class TestConfig : IServerConfig
    {
        ushort port = 34567;
        IAvatar avatar = null;

        string name = "Zorbo Server";
        string topic = "Welcome to my Zorbo Server!";

        uint banlength = 0;

        ushort maxClones = 4;
        ushort maxClients = 500;

        bool muzzledPms = true;
        bool serverbans = true;
        bool botProtection = true;
        bool showOnChannelList = true;

        Language language = Language.English;


        public ushort Port {
            get { return port; }
            set {
                if (port != value) {
                    port = value;
                    RaisePropertyChanged(() => Port);
                }
            }
        }

        public string Name {
            get { return name; }
            set {
                if (name != value) {
                    name = value;
                    RaisePropertyChanged(() => Name);
                }
            }
        }

        public string Topic {
            get { return topic; }
            set {
                if (topic != value) {
                    topic = value;
                    RaisePropertyChanged(() => Topic);
                }
            }
        }

        public uint BanLength {
            get { return banlength; }
            set {
                if (banlength != value) {
                    banlength = value;
                    RaisePropertyChanged(() => BanLength);
                }
            }
        }

        public IAvatar Avatar {
            get { return avatar; }
            set {
                if (avatar == null || !avatar.Equals(value)) {
                    avatar = value;
                    RaisePropertyChanged(() => Avatar);
                }
            }
        }

        public ushort MaxClones {
            get { return maxClones; }
            set {
                if (maxClones != value) {
                    maxClones = value;
                    RaisePropertyChanged(() => MaxClones);
                }
            }
        }

        public ushort MaxClients {
            get { return maxClients; }
            set {
                if (maxClients != value) {
                    maxClients = value;
                    RaisePropertyChanged(() => MaxClients);
                }
            }
        }

        public Language Language {
            get { return language; }
            set {
                if (language != value) {
                    language = value;
                    RaisePropertyChanged(() => Language);
                }
            }
        }

        public Boolean MuzzledPMs {
            get { return muzzledPms; }
            set {
                if (muzzledPms != value) {
                    muzzledPms = value;
                    RaisePropertyChanged(() => MuzzledPMs);
                }
            }
        }

        public Boolean ShowChannel {
            get { return showOnChannelList; }
            set {
                if (showOnChannelList != value) {
                    showOnChannelList = value;
                    RaisePropertyChanged(() => ShowChannel);
                }
            }
        }

        public Boolean BotProtection {
            get { return botProtection; }
            set {
                if (botProtection != value) {
                    botProtection = value;
                    RaisePropertyChanged(() => BotProtection);
                }
            }
        }

        public Boolean UseChatroomBansToBanServers {
            get { return serverbans; }
            set {
                if (serverbans != value) {
                    serverbans = value;
                    RaisePropertyChanged(() => UseChatroomBansToBanServers);
                }
            }
        }

        public TimeSpan ExpireOldPasswords {
            get { return TimeSpan.FromDays(90); }
            set { }
        }


        #region " INotifyPropertyChanged "

        protected virtual void RaisePropertyChanged<T>(Expression<Func<T>> property) {
            var prop = ((MemberExpression)property.Body).Member as PropertyInfo;

            if (prop == null) return;
            OnPropertyChanged(prop.Name);
        }

        protected virtual void RaisePropertyChanged(string propName) {
            OnPropertyChanged(propName);
        }

        protected virtual void OnPropertyChanged(string propName) {
            var x = PropertyChanged;
            if (x != null) x(this, new PropertyChangedEventArgs(propName));
        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
